<!--
Version 0.1
SEMF - Web Desiging Framework for Sport Event Managment
Copyright (C) 2006, SEMF Developers

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Generate CRUDS</title>


</head>

<body style="background-color:#000000; color:#00FF00">
<?php
$crud_xml = $_GET['crud_xml'];
$crud_page_xml = $_GET['crud_page_xml'];
$container_id = $_GET['container'];
$project_home = $_GET['project_home'];

$semf_root = "../../";

include_once($semf_root."page_designer/util/dirutils.php");
include_once($semf_root."page_designer/util/backup.php");

include_once("../lib/component_builder.php");

if(!file_exists($crud_xml) || !file_exists($crud_page_xml))
{
	generate_error("The configuration files doesnt exists:\n $crud_xml \n $crud_page_xml");
	exit();
}

if(!isset($container_id) || $container_id == "")
{
	generate_error("The container doesnt exists:\n ${container_id}; Please select a valid container");
	exit();
}

$crud_doc = new DOMDocument();
$crud_doc->load($crud_xml);

function generate_error($error_str)
{
	echo "<font color='red'>$error_str</font>";
}

generate_file($crud_doc->documentElement, $crud_page_xml,
					 $container_id, array(), $semf_root, $project_home);

/**
 * generate a new file from a table
 */
function generate_file($data_node, $template_file, $container_id,
						 $past_vars, $semf_root, $project_home)
{
	$data_module = $data_node->getAttribute("table_module");
	$data_table = $data_node->getAttribute("table");
	$is_array = $data_node->getAttribute("isarray");
	$op = $data_node->getAttribute("op");
	
	//give the variable a name
	$title = "";
	
	if(count($past_vars) != 0)
	{
		$data_variable = strtolower($past_vars[0]["var"]);
		$title = $past_vars[0]["table_only"];
	}
	else
	{
		$data_variable = strtolower($data_table);
		$title = $data_table;
	}
	for($k = 0; $k < count($past_vars); $k ++)
	{
		$data_variable .= "_".strtolower($past_vars[$k]["attr"]);
		$title .= " -> ".$past_vars[$k]["attr"];
	}
	

	
	$gen_list_filename = $semf_root.$project_home.$data_module."/crud_list_".$data_variable.".xml";
	$gen_edit_filename = $semf_root.$project_home.$data_module."/crud_edit_".$data_variable.".xml";
	
	$template_doc = new DOMDocument();
	$template_doc->load($template_file);
	
	$impl = new DomImplementation;
	$relative_dir = find_relative_dir($project_home.$data_module."/somefile", "page_designer/"."page.dtd");
	$dtd = $impl->createDocumentType("page", "", $relative_dir);
	$target_doc = $impl->createDocument("", "", $dtd);
	// Set other properties
	$target_doc->encoding = "UTF-8";
	$target_doc->standalone = "no";
	
	copy_node($template_doc->documentElement, $target_doc, $target_doc);
	
	$target_doc->save($gen_list_filename);
	$target_doc->save($gen_edit_filename);
	
	$gen_list_doc = new DOMDocument();
	$gen_list_doc->load($gen_list_filename);
	$gen_list_doc->validate();
	
	$gen_edit_doc = new DOMDocument();
	$gen_edit_doc->load($gen_edit_filename);
	$gen_edit_doc->validate();
	
	
	// set the title..
	$page_property_set = array("Apearance"=> 
								array("Title"=> "Listing of ".$title));
	$ret_val = change_page_property($page_property_set, "", $semf_root, $gen_list_doc, 0);
	$gen_list_doc = $ret_val["doc"];
	
	
	// set the title.. for editing..
	$page_property_set = array("Apearance"=> 
								array("Title"=> "Editing of ".$title));
	$ret_val = change_page_property($page_property_set, "", $semf_root, $gen_edit_doc, 0);
	$gen_edit_doc = $ret_val["doc"];
	
	//generate the containers..
	//e.g.: new_grid_component($gen_list_doc, $container_id, 3, 4, array(5,3,4), false, 0);
	
	echo ">Generate ".$gen_list_filename."<br/>";
	flush();
	
	if(count($past_vars) == 0 || $is_array == "yes")
	{
		generate_multiple_item_listing($data_node, $gen_list_doc, $container_id, $title,
			$template_file, $data_variable, $past_vars, $semf_root, $project_home);
	}
	else //not an array
	{
		generate_single_item_listing($data_node, $gen_list_doc, $container_id, $title,
			$template_file, $data_variable, $past_vars, $semf_root, $project_home);
	}
	
	echo ">Generate ".$gen_edit_filename."<br/>";
	flush();
	
	//for all the files..
	if($op == "new")
	{
		generate_single_item_editing($data_node, $gen_edit_doc, $container_id, $title,
			$template_file, $data_variable, $past_vars, $semf_root, $project_home);
	}
	else
	{
		if($is_array == "yes")
		{
			generate_multiple_item_editing_from_existing(
				$data_node, $gen_edit_doc, $container_id, $title,
				$template_file, $data_variable, $past_vars, $semf_root, $project_home);
		}
		else
		{
			generate_single_item_editing_from_existing(
				$data_node, $gen_edit_doc, $container_id, $title,
				$template_file, $data_variable, $past_vars, $semf_root, $project_home);
		}
		
	}
			
	savePageXML($gen_edit_doc, $gen_edit_filename);
	savePageXML($gen_list_doc, $gen_list_filename);
}


/**
 * create new text with value
 */
function new_grid_component($page_doc, $container, $rows, $cols, $heights, $absolute_values, $no_overflow, $border_color, $bg_color)
{
	$property_set = array('Apearance' => array(
							'Rows'=> $rows,
							'Columns'=> 1, //first create single column
							'No_Overflow'=> $no_overflow)
							);
	
	$semf_root = "../../";
	//recalculate the heights in same proportion to fit to the outer container..
	$total_height = 0;
	
	$non_set_values = array();
	
	for($i = 0; $i < count($heights); $i ++)
	{
		if($heights[$i] != -1)
		{
			$total_height += $heights[$i];
		}
		else //only happen for abslute values..
		{
			$non_set_values[count($non_set_values)] = $i;
		}
	}
	
	$container_ele = $page_doc->getElementById($container);
	$container_height = $container_ele->getAttribute("height");
	
	if($absolute_values)
	{
		$non_set_height = ($container_height - $total_height)/count($non_set_values);
		foreach($non_set_values as $non_set_value)
		{
			$heights[$non_set_value] = $non_set_height;
		}
	}
	
	for($i = 0; $i < count($heights)-1 && $total_height != 0; $i ++)
	{
		if($heights[$i] == -1)
		{
			continue;
		}
		if(!$absolute_values)
		{
			$heights[$i] = $heights[$i]*$container_height/$total_height;
		}
		$key = "Height-Row".$i;
		$value = $heights[$i];
		
		$property_set['Apearance'][$key] = $value;
		
	}
	
		
	$return_value = build_component("Grid", $property_set, $container, $semf_root, $page_doc);
	$outer_containers = $return_value["containers"];
	$page_doc = $return_value["doc"];
	//the outer containers are meant to be from top to bottom..
	
	$inner_containers = array();
	
	$property_set = array('Apearance' => array(
							'Rows'=> 1,
							'Columns'=> $cols,
							'No_Overflow'=> $no_overflow),
						'DataBinding' => array(
							'Border-Color'=> $border_color,
							'Background-Color'=> $bg_color
								));
							
	for($i = 0; $i < count($outer_containers); $i++)
	{
		$outer_container = $outer_containers[$i];
		$return_value = build_component("Grid", $property_set, $outer_container,
								$semf_root, $page_doc);
		$return_containers = $return_value["containers"];
		$inner_containers = array_merge($inner_containers, $return_containers);
		$page_doc = $return_value["doc"];
	}
	
	return array("containers"=>$inner_containers,
					"doc"=> $page_doc); //there should be rows * cols entries..
}

function report_error($str)
{
	echo "ERROR: ${str}<br/>\n";
}

function generate_image_file($template_file, $container_id, $data_module, 
					$semf_root, $project_home)
{
	$gen_image_viewer = $semf_root.$project_home.$data_module."/crud_image_viewer.xml";
	
	
	if(file_exists($gen_image_viewer)) return;
	
	$template_doc = new DOMDocument();
	$template_doc->load($template_file);
	
	$impl = new DomImplementation;
	$relative_dir = find_relative_dir($project_home.$data_module."/somefile", "page_designer/"."page.dtd");
	$dtd = $impl->createDocumentType("page", "", $relative_dir);
	$target_doc = $impl->createDocument("", "", $dtd);
	// Set other properties
	$target_doc->encoding = "UTF-8";
	$target_doc->standalone = "no";
	
	copy_node($template_doc->documentElement, $target_doc, $target_doc);
	
	$target_doc->save($gen_image_viewer);
	
	//load containers..
	
	$window_property_list = array("DataBinding"=> 
									array("Title"=> "\$title"));
	$window_retval = build_component("Window", $window_property_list, $container_id, $semf_root, $gen_image_viewer);
	
	$window_container = $window_retval["containers"];
	
	$image_property_set = array("DataBinding"=> array("Image"=> "\$src"));
	build_component("Image", $image_property_set,
		 $window_container[0], $semf_root, $gen_image_viewer);
	
}

function generate_multiple_item_listing($data_node, $gen_list_doc, $container_id, $title,
			$template_file, $data_variable, $past_vars, $semf_root, $project_home)
{	
	
	$data_module = $data_node->getAttribute("table_module");
	$data_table = $data_node->getAttribute("table");
	
	$op = $data_node->getAttribute("op");
	
	//creating the window to listing
	$ret_val = new_grid_component($gen_list_doc, $container_id, 2, 1, array(-1,50), true, 0, '', '');
	$containers = $ret_val["containers"];
	$gen_list_doc = $ret_val["doc"];
	$window_outer_container = $containers[0];
	$button_outer_container = $containers[1];
	
	$ret_val = new_grid_component($gen_list_doc, $button_outer_container, 1, 3, array(1), false, 0, '','');
	$button_containers = $ret_val["containers"];
	$gen_list_doc = $ret_val["doc"];
	
	$window_property_list = array("DataBinding"=> 
									array("Title"=> "Listing of ".$title));
	$window_retval = build_component("Window", $window_property_list, $window_outer_container, $semf_root, $gen_list_doc);
	
	$window_container = $window_retval["containers"];
	$gen_list_doc = $window_retval["doc"];
	
	$field_nodes = $data_node->childNodes;
	$fields_count = $field_nodes->length;
	
	// filling titles to windows top menu..
	$ret_val = new_grid_component($gen_list_doc, $window_container[0], 2, 1, array(30, -1), true, 0,'','');
	$containers = $ret_val["containers"];
	$gen_list_doc = $ret_val["doc"];
	
	$title_container = $containers[0];
	$content_container = $containers[1];
	
	// filling the content window..
	$field_containers_count = $fields_count;
	if($op == "new")
	{
		$field_containers_count ++;
	}
	$ret_val = new_grid_component($gen_list_doc, $title_container, 1, $field_containers_count, array(1), false, 0, '#66CCCC', '#CCFFCC');
	$field_title_containers = $ret_val["containers"];
	$gen_list_doc = $ret_val["doc"];
	
	$repeater_property_set = array("Apearance"=> array("Height"=> 22));
	$repeater_retval = build_component("Repeater", $repeater_property_set, $content_container, $semf_root, $gen_list_doc);
	$repeater_container = $repeater_retval["containers"];
	$gen_list_doc = $ret_val["doc"];
	
	$ret_val = new_grid_component($gen_list_doc, 
					$repeater_container[0], 1, $field_containers_count, array(1), 
					false, 1,'#66CCCC','');
	$field_content_containers = $ret_val["containers"];
	$gen_list_doc = $ret_val["doc"];
	
	for($i = 0, $field_node = $data_node->firstChild; $i < $fields_count && $field_node;
					$i ++, $field_node = $field_node->nextSibling)
	{
		//set the $ith title
		if($op == "new")
		{
			$field_title_container = $field_title_containers[$i+1];
		}
		else
		{
			$field_title_container = $field_title_containers[$i];
		}
		$field_name = $field_node->getAttribute("name");
		
		
		$field_title_property_set = array("DataBinding"=> array("Text"=> $field_name));
		$ret_val = build_component("Label", $field_title_property_set, $field_title_container, $semf_root, $gen_list_doc);
		$gen_list_doc = $ret_val["doc"];
		
		//just for now build the content same as for the title..
		if($op == "new")
		{
			$field_content_container = $field_content_containers[$i+1];
		}
		else
		{
			$field_content_container = $field_content_containers[$i];
		}
		//now check the field type..
		$field_type = $field_node->getAttribute("type");
		$field_name = $field_node->getAttribute("name");
		
		if($field_type == "Text" || $field_type == "Number" || $field_type == "Numeric" ||
			$field_type == "Date" || $field_type == "Time")
		{
			$db_code = "@".$data_variable.".".$field_name;
			$field_content_property_set = array("DataBinding"=> array("Text"=> $db_code));
			$ret_val = build_component("Label", $field_content_property_set,
				 $field_content_container, $semf_root, $gen_list_doc);
			
			$gen_list_doc = $ret_val["doc"];
		}
		elseif($field_type == "Image")
		{
			$db_code = "@".$data_variable.".".$field_name;
			$image_title = $title . " -> ".$field_name;
			$field_link_property_set = array("DataBinding"=> 
											array("Text"=> "".$field_name,
												"Title"=> "".$field_name),
											"Links"=>
											array("Link"=> $data_module.".crud_image_viewer",
												"Parameter-Count"=> 2,
												"Link-Param1"=> "src=".$db_code,
												"Link-Param2"=> "title='".$image_title."'"));
			$ret_val = build_component("Link", $field_link_property_set,
				 $field_content_container, $semf_root, $gen_list_doc);
			$gen_list_doc = $ret_val["doc"];
			
			generate_image_file($template_file, $container_id, $data_module, 
					$semf_root, $project_home);
		}
		else
		{
			$field_table_module = $field_node->getAttribute("table_module");
			$field_table = $field_node->getAttribute("table");
			if($field_table_module == "" || $field_table == "")
			{
				//some problem..
				report_error("Unknown Type.. ${field_type}");
				exit();
			}
			//add a link to view the table module..
			//new page name..
			$new_var_id = count($past_vars);
			$new_page_vars = $past_vars;
			
			$new_page_vars[$new_var_id] = array("table"=> $data_module.".".$data_table,
											"table_only"=> $data_table,
											"var"=> $data_variable,
											"attr"=> $field_name);
								
			$new_var_list_file_ptr = $field_table_module.".crud_list_".
									$data_variable."_".strtolower($field_name);
			$field_link_property_set = array("DataBinding"=> 
											array("Text"=> "".$field_name,
												"Title"=> "".$field_name),
											"Links"=>
											array("Link"=> $new_var_list_file_ptr,
												"Parameter-Count"=> count($past_vars) + 1));
			for($j = 0; $j < count($new_page_vars); $j ++)
			{
				$link_param_index = $j + 1;
				if($j == count($new_page_vars) -1)
				{
					$field_link_property_set["Links"]["Link-Param".$link_param_index] = 
						$new_page_vars[$j]["var"]."= @".$new_page_vars[$j]["var"].".id";
				}
				else
				{
					$field_link_property_set["Links"]["Link-Param".$link_param_index] = 
						$new_page_vars[$j]["var"]."= $".$new_page_vars[$j]["var"];
					
				}
			}
			$ret_val = build_component("Link", $field_link_property_set,
				 $field_content_container, $semf_root, $gen_list_doc);
			$gen_list_doc = $ret_val["doc"];
			generate_file($field_node, $template_file, $container_id,
						 $new_page_vars, $semf_root, $project_home);
		}
		
	}
	//set the radio field
	
	if($op == "new")
	{
		$radio_container = $field_content_containers[0]; //the very first field container.
		$radio_content_property_set = array(
							"Apearance"=> array("Single"=> "1",
												"Hidden_label"=> "1"),
							"DataBinding"=> array("List"=> "@${data_variable}",
													"Value"=> "\$selected_${data_variable}"));
		$ret_val = build_component("RadioGroup", $radio_content_property_set, $radio_container, $semf_root, $gen_list_doc);
		$gen_list_doc = $ret_val["doc"];
	}
	
	//change the page on load properties on .. listings
	//now judge the startup code..
	$startup_code = "";

	$startup_code .= "SELECT FROM ${data_module}.${data_table} @${data_variable}";
	if(count($past_vars) > 0)
	{
		$l = count($past_vars) -1;
		$lth_table = $past_vars[$l]["table"];
		$lth_var = $past_vars[$l]["var"];
		$lth_attr = $past_vars[$l]["attr"];
		$startup_code .= " WHERE @${lth_var}.${lth_attr}=@${data_variable}";
		
		$startup_code = "SELECT FROM ${lth_table} @${lth_var} WHERE ".
							"@${lth_var}=\$${lth_var};\n". 
									$startup_code; //something to add first
	}
	
	$startup_code .= ";\n";
	$startup_code .= "if(\$selected_${data_variable} == NULL)\n";
	$startup_code .= "{\n";
	$startup_code .= "  \$selected_${data_variable} = @${data_variable};\n";
	$startup_code .= "}\n";
	
	// page properties..
	$change_string = "CRUD GENERATED CODE";
	$page_property_set = array("Action Handlers"=> 
								array("onLoad"=> $startup_code));
	$ret_val = change_page_property($page_property_set, $change_string, $semf_root, $gen_list_doc, 0);
	$gen_list_doc = $ret_val["doc"];
	
	// button containers..
	// take 1st button as "Add New";
	if($op == "new")
	{
		$button_property_set = array("DataBinding"=> array(
										"Text"=> "Add New"));
		$ret_val = build_component("Button", $button_property_set,
					 $button_containers[0], $semf_root, $gen_list_doc);
		$add_new_btn_id = $ret_val['component'];
		$gen_list_doc = $ret_val["doc"];
		// Add New code..
		$add_new_code = $list_update_code = "";
		$new_data_variable = "new_".$data_variable;
		$add_new_code .= "decl @${new_data_variable} = new ${data_module}.${data_table};\n";
		
		$list_update_code .= "SELECT FROM ${data_module}.${data_table} @${data_variable}";
		
		if(count($past_vars) > 0)
		{
			$l = count($past_vars) -1;
			$lth_table = $past_vars[$l]["table"];
			$lth_var = $past_vars[$l]["var"];
			$lth_attr = $past_vars[$l]["attr"];
			$add_new_code .= "@${lth_var}.${lth_attr} += @${new_data_variable};\n\n";
			
			$list_update_code .= " WHERE @${lth_var}.${lth_attr}=@${data_variable};\n";	
		}
		else
		{
			$list_update_code .= ";\n";
		}
		$add_new_code .= $list_update_code;
		
		$add_new_code .= "\$selected_${data_variable} = @${new_data_variable};\n";
		$params_list = "\"${data_variable}\", \$selected_${data_variable}";
		if(count($past_vars) > 0)
		{
			$l = count($past_vars) -1;
			$lth_var = $past_vars[$l]["var"];
			$params_list .= ", \"${lth_var}\", \$${lth_var}";
		}
		$add_new_code .= "go2(\"${data_module}.crud_edit_${data_variable}\", ${params_list});\n";
		
	
		//setting the add new code..
		$change_string = "CRUD GENERATED CODE";
		$code_property_set = array("Action Handlers"=> 
									array("onClick"=> $add_new_code));
		$ret_val = change_component_property("Button", $code_property_set, $add_new_btn_id,
										 $change_string, $semf_root, $gen_list_doc, 0);
		$gen_list_doc = $ret_val["doc"];
		
		//take 2nd button as delete..
		$button_property_set = array("DataBinding"=> array(
										"Text"=> "Delete"));
		$ret_val = build_component("Button", $button_property_set,
					 $button_containers[1], $semf_root, $gen_list_doc);
		$delete_btn_id = $ret_val['component'];
		$gen_list_doc = $ret_val["doc"];
		
		// Delete code..
		$delete_code = $list_update_code = "";
		$selected_variable = "selected_".$data_variable;
		$delete_code .= "SELECT FROM ${data_module}.${data_table} @${selected_variable} WHERE @${selected_variable} = \$${selected_variable};\n";
		
		$list_update_code .= "SELECT FROM ${data_module}.${data_table} @${data_variable}";
		
		if(count($past_vars) > 0)
		{
			$l = count($past_vars) -1;
			$lth_table = $past_vars[$l]["table"];
			$lth_var = $past_vars[$l]["var"];
			$lth_attr = $past_vars[$l]["attr"];
			$delete_code .= "@${lth_var}.${lth_attr} -= @${selected_variable};\n\n";
			
			$list_update_code .= " WHERE @${lth_var}.${lth_attr}=@${data_variable};\n";	
		}
		else
		{
			$list_update_code .= ";\n";
		}
		$delete_code .= "DELETE @${selected_variable};\n";
		$delete_code .= $list_update_code;
		
		$delete_code .= "\$selected_${data_variable} = @${data_variable};\n";
	
		$change_string = "CRUD GENERATED CODE";
		$code_property_set = array("Action Handlers"=> 
									array("onClick"=> $delete_code));
		$ret_val = change_component_property("Button", $code_property_set, $delete_btn_id, $change_string,
							 $semf_root, $gen_list_doc, 0);
		$gen_list_doc = $ret_val["doc"];
	}
		
	//take 3rd button as Edit..
	$button_property_set = array("DataBinding"=> array(
									"Text"=> "Edit"));
	$ret_val = build_component("Button", $button_property_set,
				 $button_containers[2], $semf_root, $gen_list_doc);
	$gen_list_doc = $ret_val["doc"];
	
	$edit_btn_id = $ret_val['component'];
	
	$edit_code = "";
	$params_list = "\"${data_variable}\", \$selected_${data_variable}";
	if(count($past_vars) > 0)
	{
		$l = count($past_vars) -1;
		$lth_var = $past_vars[$l]["var"];
		$params_list .= ", \"${lth_var}\", \$${lth_var}";
	}
	$edit_code .= "go2(\"${data_module}.crud_edit_${data_variable}\", ${params_list});\n";
	
	$change_string = "CRUD GENERATED CODE";
	$code_property_set = array("Action Handlers"=> 
								array("onClick"=> $edit_code));
	$ret_val = change_component_property("Button", $code_property_set, $edit_btn_id, $change_string,
						 $semf_root, $gen_list_doc, 0);
	$gen_list_doc = $ret_val["doc"];
}

function generate_single_item_listing($data_node, $gen_list_doc, $container_id, $title,
			$template_file, $data_variable, $past_vars, $semf_root, $project_home)
{
	$data_module = $data_node->getAttribute("table_module");
	$data_table = $data_node->getAttribute("table");
	$rows = 0;
	$row_heights = array();
	$field_nodes = $data_node->childNodes;
	foreach($field_nodes as $field_node)
	{
		$field_type = $field_node->getAttribute("type");
		if($field_type == "Image")
		{
			$row_heights[$rows] = 3;
			$rows ++;
		}
		$row_heights[$rows] = 1;
		$rows ++;
	}
	
	//now create the grid
	$ret_val = new_grid_component($gen_list_doc, $container_id, 2, 1, array(-1,50), true, 0,'','');
	$containers = $ret_val["containers"];
	$gen_list_doc = $ret_val["doc"];
	$window_outer_container = $containers[0];
	$button_outer_container = $containers[1];
	
	$ret_val = new_grid_component($gen_list_doc, $button_outer_container, 1, 3, array(1), false, 0,'', '');
	$button_containers = $ret_val["containers"];
	$gen_list_doc = $ret_val["doc"];
	
	$window_property_list = array("DataBinding"=> 
									array("Title"=> "Listing of ".$title));
	$window_retval = build_component("Window", $window_property_list, $window_outer_container, $semf_root, $gen_list_doc);
	
	$window_container = $window_retval["containers"];
	$gen_list_doc = $window_retval["doc"];
	
	$ret_val = new_grid_component($gen_list_doc, $window_container[0], $rows, 2, $row_heights, false, 0, '#66CCCC', '');
	$containers = $ret_val["containers"];
	$gen_list_doc = $ret_val["doc"];
	
	$i = 0;
	foreach($field_nodes as $field_node)
	{
		//create a bg control..
		$property_set = array("Apearance"=> array(
									"Color"=> '#CCFFCC'));		
		$ret_val = build_component("Background", $property_set, $containers[$i], $semf_root, $gen_list_doc);
		$gen_list_doc = $ret_val["doc"];
		$label_containers = $ret_val['containers'];
		
		$field_name = $field_node->getAttribute("name");
		$property_set = array("DataBinding"=> array(
									"Text"=> $field_name));
									
		$ret_val = build_component("Label", $property_set, $label_containers[0], $semf_root, $gen_list_doc);
		$gen_list_doc = $ret_val["doc"];
		$i ++;
		
		$field_content_container = $containers[$i];
		$i ++;
		
		$field_type = $field_node->getAttribute("type");
		$field_name = $field_node->getAttribute("name");
		
		
		if($field_type == "Text" || $field_type == "Number" || $field_type == "Numeric" ||
			$field_type == "Date" || $field_type == "Time")
		{
			$db_code = "@".$data_variable.".".$field_name;
			$field_content_property_set = array("DataBinding"=> array("Text"=> $db_code));
			$ret_val = build_component("Label", $field_content_property_set,
				 $field_content_container, $semf_root, $gen_list_doc);
			$gen_list_doc = $ret_val["doc"];
		}
		elseif($field_type == "Image")
		{
			//show the image here..
			$db_code = "@".$data_variable.".".$field_name;
			$field_content_property_set = array("DataBinding"=> array("Image"=> $db_code));
			$ret_val = build_component("Image", $field_content_property_set,
				 $field_content_container, $semf_root, $gen_list_doc);
			$gen_list_doc = $ret_val["doc"];
			$i ++;
			$second_field_content_container = $containers[$i];
			$i ++;
			$field_content_property_set = array("DataBinding"=> array("Text"=> $db_code));
			$ret_val = build_component("Label", $field_content_property_set,
				 $second_field_content_container, $semf_root, $gen_list_doc);
			$gen_list_doc = $ret_val["doc"];
		}
		else
		{
			$field_table_module = $field_node->getAttribute("table_module");
			$field_table = $field_node->getAttribute("table");
			if($field_table_module == "" || $field_table == "")
			{
				//some problem..
				report_error("Unknown Type.. ${field_type}");
				exit();
			}
			//add a link to view the table module..
			//new page name..
			$new_var_id = count($past_vars);
			$new_page_vars = $past_vars;
			
			$new_page_vars[$new_var_id] = array("table"=> $data_module.".".$data_table,
												"table_only"=> $data_table,
											"var"=> $data_variable,
											"attr"=> $field_name);
								
			$new_var_list_file_ptr = $field_table_module.".crud_list_".
									$data_variable."_".strtolower($field_name);
			$field_link_property_set = array("DataBinding"=> 
											array("Text"=> "".$field_name,
												"Title"=> "".$field_name),
											"Links"=>
											array("Link"=> $new_var_list_file_ptr,
												"Parameter-Count"=> count($past_vars) + 1));
			for($j = 0; $j < count($new_page_vars); $j ++)
			{
				$link_param_index = $j + 1;
				if($j == count($new_page_vars) -1)
				{
					$field_link_property_set["Links"]["Link-Param".$link_param_index] = 
						$new_page_vars[$j]["var"]."= @".$new_page_vars[$j]["var"].".id";
				}
				else
				{
					$field_link_property_set["Links"]["Link-Param".$link_param_index] = 
						$new_page_vars[$j]["var"]."= $".$new_page_vars[$j]["var"];
					
				}
			}
			$ret_val = build_component("Link", $field_link_property_set,
				 $field_content_container, $semf_root, $gen_list_doc);
			$gen_list_doc = $ret_val["doc"];
			generate_file($field_node, $template_file, $container_id,
						 $new_page_vars, $semf_root, $project_home);
		}
	}
	//change the page on load properties on .. listings
	//now judge the startup code..
	$startup_code = "";

	$startup_code .= "SELECT FROM ${data_module}.${data_table} @${data_variable}";
	
	//here we should have exactly one item..
	$exist_check_code = "";
	if(count($past_vars) > 0)
	{
		$l = count($past_vars) -1;
		$lth_table = $past_vars[$l]["table"];
		$lth_var = $past_vars[$l]["var"];
		$lth_attr = $past_vars[$l]["attr"];
		$startup_code .= " WHERE @${lth_var}.${lth_attr}=@${data_variable}";
		
		$startup_code = "SELECT FROM ${lth_table} @${lth_var} WHERE ".
							"@${lth_var}=\$${lth_var};\n". 
									$startup_code; //something to add first
		$exist_check_code .= "\$exist = count(@${data_variable});\n";
		$exist_check_code .= "if(\$exist == 0)\n";
		$exist_check_code .= "{\n";
		$exist_check_code .= "  DECL @${data_variable} = new ${data_module}.${data_table};\n";
		$exist_check_code .= "  @${lth_var}.${lth_attr}=@${data_variable};\n";
		$exist_check_code .= "}\n";
	}
	
	$startup_code .= ";\n";
	$startup_code .= $exist_check_code;
	$startup_code .= "if(\$selected_${data_variable} == NULL)\n";
	$startup_code .= "{\n";
	$startup_code .= "  \$selected_${data_variable} = @${data_variable};\n";
	$startup_code .= "}\n";
	
	// page properties..
	$change_string = "CRUD GENERATED CODE";
	$page_property_set = array("Action Handlers"=> 
								array("onLoad"=> $startup_code));
	$ret_val = change_page_property($page_property_set, $change_string, $semf_root, $gen_list_doc, 0);
	$gen_list_doc = $ret_val["doc"];
	
	
	//take 3rd container as Edit..
	$button_property_set = array("DataBinding"=> array(
									"Text"=> "Edit"));
	$ret_val = build_component("Button", $button_property_set,
				 $button_containers[2], $semf_root, $gen_list_doc);
	$gen_list_doc = $ret_val["doc"];
	
	$edit_btn_id = $ret_val['component'];
	
	$edit_code = "";
	$edit_code .= "\$selected_${data_variable} = @${data_variable};\n";
	$params_list = "\"${data_variable}\", \$selected_${data_variable}";
	if(count($past_vars) > 0)
	{
		$l = count($past_vars) -1;
		$lth_var = $past_vars[$l]["var"];
		$params_list .= ", \"${lth_var}\", \$${lth_var}";
	}
	$edit_code .= "go2(\"${data_module}.crud_edit_${data_variable}\", ${params_list});\n";
	
	$change_string = "CRUD GENERATED CODE";
	$code_property_set = array("Action Handlers"=> 
								array("onClick"=> $edit_code));
	$ret_val = change_component_property("Button", $code_property_set, $edit_btn_id, $change_string,
						 $semf_root, $gen_list_doc, 0);
	$gen_list_doc = $ret_val["doc"];
}

function generate_single_item_editing($data_node, $gen_edit_doc, $container_id, $title,
			$template_file, $data_variable, $past_vars, $semf_root, $project_home)
{
	$data_module = $data_node->getAttribute("table_module");
	$data_table = $data_node->getAttribute("table");
	$is_array = $data_node->getAttribute("isarray");
	$rows = 0;
	$row_heights = array();
	$field_nodes = $data_node->childNodes;
	foreach($field_nodes as $field_node)
	{
		$field_type = $field_node->getAttribute("type");
		if($field_type == "Image")
		{
			$row_heights[$rows] = 3;
			$rows ++;
		}
		$row_heights[$rows] = 1;
		$rows ++;
	}
	
	//now create the grid
	$ret_val = new_grid_component($gen_edit_doc, $container_id, 2, 1, array(-1,50), true, 0,'','');
	$containers = $ret_val["containers"];
	$gen_edit_doc = $ret_val["doc"];
	$window_outer_container = $containers[0];
	$button_outer_container = $containers[1];
	
	$ret_val = new_grid_component($gen_edit_doc, $button_outer_container, 1, 3, array(1), false, 0, '', '');
	$button_containers = $ret_val["containers"];
	$gen_edit_doc = $ret_val["doc"];
	
	$window_property_list = array("DataBinding"=> 
									array("Title"=> "Editing of ".$title));
	$window_retval = build_component("Window", $window_property_list, $window_outer_container, $semf_root, $gen_edit_doc);
	
	$window_container = $window_retval["containers"];
	$gen_edit_doc = $window_retval["doc"];
	
	$ret_val = new_grid_component($gen_edit_doc, $window_container[0],
								 $rows, 2, $row_heights, false, 0, '#66CCCC', '');
	$containers = $ret_val["containers"];
	$gen_edit_doc = $ret_val["doc"];
	
	$i = 0;
	
	$init_code = "";
	$submit_code = "";
	foreach($field_nodes as $field_node)
	{
		//create a bg control..
		$property_set = array("Apearance"=> array(
									"Color"=> '#CCFFCC'));		
		$ret_val = build_component("Background", $property_set, $containers[$i], $semf_root, $gen_edit_doc);
		$gen_edit_doc = $ret_val["doc"];
		$label_containers = $ret_val['containers'];
		
		$field_name = $field_node->getAttribute("name");
		$property_set = array("DataBinding"=> array(
									"Text"=> $field_name));
									
		$ret_val = build_component("Label", $property_set, $label_containers[0], $semf_root, $gen_edit_doc);
		$gen_edit_doc = $ret_val["doc"];
		$i ++;
		
		$field_content_container = $containers[$i];
		$i ++;
		
		$field_type = $field_node->getAttribute("type");
		$field_name = $field_node->getAttribute("name");
		
		if($field_type == "Text" || $field_type == "Number" || $field_type == "Numeric" || $field_type == "Time")
		{
			$db_code = "\$".$field_name;
			$init_code .= "\$${field_name} = @${data_variable}.${field_name};\n";
			$submit_code .= "@${data_variable}.${field_name} = \$${field_name};\n";
			
			$field_content_property_set = array("DataBinding"=> array("Text"=> $db_code));
			$ret_val = build_component("TextBox", $field_content_property_set,
				 $field_content_container, $semf_root, $gen_edit_doc);
			
			$gen_edit_doc = $ret_val["doc"];		

		}
		elseif($field_type == "Date" )
		{
			$db_code = "\$".$field_name;
			$init_code .= "\$${field_name} = @${data_variable}.${field_name};\n";
			$init_code .= "\$${field_name}_year = get_year(\$${field_name});\n";
			$init_code .= "\$${field_name}_month = get_month(\$${field_name});\n";
			$init_code .= "\$${field_name}_day = get_day(\$${field_name});\n";
			
			$submit_code .= "\$${field_name} = new_date(\$${field_name}_year, \$${field_name}_month, \$${field_name}_day);\n";
			$submit_code .= "@${data_variable}.${field_name} = \$${field_name};\n";
			
			$field_content_property_set = array("DataBinding"=> 
										array("Year"=> "\$${field_name}_year",
												"Month"=> "\$${field_name}_month",
												"Day"=> "\$${field_name}_day"));
			$ret_val = build_component("Calender", $field_content_property_set,
				 $field_content_container, $semf_root, $gen_edit_doc);
			
			$gen_edit_doc = $ret_val["doc"];
		}
		elseif($field_type == "Image")
		{
			//show the image here..
			$db_code = "\$".$field_name;
			$init_code .= "\$${field_name} = @${data_variable}.${field_name};\n";
			$submit_code .= "@${data_variable}.${field_name} = \$${field_name};\n";
			
			$field_content_property_set = array("DataBinding"=> array("Image"=> $db_code));
			$ret_val = build_component("Image", $field_content_property_set,
				 $field_content_container, $semf_root, $gen_edit_doc);
			$gen_edit_doc = $ret_val["doc"];
			$i ++;
			$second_field_content_container = $containers[$i];
			$i ++;
			$field_content_property_set = array("DataBinding"=> array("File Name"=> $db_code,
																"Directory"=> "../store/images/"));
			$ret_val = build_component("Uploader", $field_content_property_set,
				 $second_field_content_container, $semf_root, $gen_edit_doc);
			$gen_edit_doc = $ret_val["doc"];
		}
		else
		{
			$field_table_module = $field_node->getAttribute("table_module");
			$field_table = $field_node->getAttribute("table");
			if($field_table_module == "" || $field_table == "")
			{
				//some problem..
				report_error("Unknown Type.. ${field_type}");
				exit();
			}
			//add a link to view the table module..
			//new page name..
			$new_var_id = count($past_vars);
			$new_page_vars = $past_vars;
			
			$new_page_vars[$new_var_id] = array("table"=> $data_module.".".$data_table,
												"table_only"=> $data_table,
											"var"=> $data_variable,
											"attr"=> $field_name);
								
			$new_var_list_file_ptr = $field_table_module.".crud_edit_".
									$data_variable."_".strtolower($field_name);
			$field_link_property_set = array("DataBinding"=> 
											array("Text"=> "".$field_name,
												"Title"=> "".$field_name),
											"Links"=>
											array("Link"=> $new_var_list_file_ptr,
												"Parameter-Count"=> count($past_vars) + 1));
			for($j = 0; $j < count($new_page_vars); $j ++)
			{
				$link_param_index = $j + 1;
				if($j == count($new_page_vars) -1)
				{
					$field_link_property_set["Links"]["Link-Param".$link_param_index] = 
						$new_page_vars[$j]["var"]."= @".$new_page_vars[$j]["var"].".id";
				}
				else
				{
					$field_link_property_set["Links"]["Link-Param".$link_param_index] = 
						$new_page_vars[$j]["var"]."= $".$new_page_vars[$j]["var"];
					
				}
			}
			$ret_val = build_component("Link", $field_link_property_set,
				 $field_content_container, $semf_root, $gen_edit_doc);
			$gen_edit_doc = $ret_val["doc"];
			/*generate_file($field_node, $template_file, $container_id,
						 $new_page_vars, $semf_root, $project_home);*/
		}
	}
	//change the onLoad properties on editing..

	$startup_code = "";
	$startup_code .= "SELECT FROM ${data_module}.${data_table} @${data_variable} ".
					"WHERE @${data_variable} = \$${data_variable};\n";
	
	//here we should have exactly one item..
	$exist_check_code = "";
	if(count($past_vars) > 0)
	{
		$l = count($past_vars) -1;
		$lth_table = $past_vars[$l]["table"];
		$lth_var = $past_vars[$l]["var"];
		$lth_attr = $past_vars[$l]["attr"];
		
		$exist_check_code .= "if(\$${data_variable} == NULL)\n";
		$exist_check_code .= "{\n";
		$exist_check_code .= "  SELECT FROM ${lth_table} @${lth_var} ".
				"WHERE @${lth_var} = \$${lth_var};\n";
		$exist_check_code .= "  SELECT FROM ${data_module}.${data_table} @${data_variable} WHERE @${lth_var}.${lth_attr}=@${data_variable};\n";
		if($is_array != "yes") //not for array..
		{
			$exist_check_code .= "  \$exist = count(@${data_variable});\n";
			$exist_check_code .= "  if(\$exist == 0)\n";
			$exist_check_code .= "  {\n";
			$exist_check_code .= "    DECL @${data_variable} = new ${data_module}.${data_table};\n";
			$exist_check_code .= "    @${lth_var}.${lth_attr}=@${data_variable};\n";
			$exist_check_code .= "  }\n";	
		}
		$exist_check_code .= "}\n";
		$exist_check_code .= "else\n";
		$exist_check_code .= "{\n";
		$exist_check_code .= "  ".$startup_code;
		$exist_check_code .= "}\n";
		
		$startup_code = $exist_check_code;
	}
	
	$startup_code .= $init_code;
	
	// page properties..
	$change_string = "CRUD GENERATED CODE";
	$page_property_set = array("Action Handlers"=> 
								array("onLoad"=> $startup_code));
	$ret_val = change_page_property($page_property_set, $change_string,
									 $semf_root, $gen_edit_doc, 0);
	$gen_edit_doc = $ret_val["doc"];
	
	//take 1st container to reset..
	$button_property_set = array("DataBinding"=> array(
									"Text"=> "Reset"));
	$ret_val = build_component("Button", $button_property_set,
				 $button_containers[0], $semf_root, $gen_edit_doc);
	$gen_list_doc = $ret_val["doc"];
	
	$reset_btn_id = $ret_val['component'];
	
	$change_string = "CRUD GENERATED CODE";
	$code_property_set = array("Action Handlers"=> 
								array("onClick"=> $init_code));
	$ret_val = change_component_property("Button", $code_property_set, $reset_btn_id,
						 $change_string,
						 $semf_root, $gen_edit_doc, 0);
	$gen_edit_doc = $ret_val["doc"];
	
		
	//take 3rd container as Submit..
	$button_property_set = array("DataBinding"=> array(
									"Text"=> "Submit"));
	$ret_val = build_component("Button", $button_property_set,
				 $button_containers[2], $semf_root, $gen_edit_doc);
	$gen_list_doc = $ret_val["doc"];
	
	$submit_btn_id = $ret_val['component'];
	
	$params_list = "\"${data_variable}\", \$selected_${data_variable}";
	if(count($past_vars) > 0)
	{
		$l = count($past_vars) -1;
		$lth_var = $past_vars[$l]["var"];
		$params_list .= ", \"${lth_var}\", \$${lth_var}";
	}
	$submit_code .= "go2(\"${data_module}.crud_list_${data_variable}\", ${params_list});\n";
	
	$change_string = "CRUD GENERATED CODE";
	$code_property_set = array("Action Handlers"=> 
								array("onClick"=> $submit_code));
	$ret_val = change_component_property("Button", $code_property_set,
						 $submit_btn_id, $change_string,
						 $semf_root, $gen_edit_doc, 0);
	$gen_edit_doc = $ret_val["doc"];
	
}


function generate_single_item_editing_from_existing($data_node, $gen_list_doc, $container_id, $title,
			$template_file, $data_variable, $past_vars, $semf_root, $project_home)
{
	
	$data_module = $data_node->getAttribute("table_module");
	$data_table = $data_node->getAttribute("table");
	
	//creating the window to listing
	$ret_val = new_grid_component($gen_list_doc, $container_id, 2, 1, array(-1,50), true, 0, '', '');
	$containers = $ret_val["containers"];
	$gen_list_doc = $ret_val["doc"];
	$window_outer_container = $containers[0];
	$button_outer_container = $containers[1];
	
	$ret_val = new_grid_component($gen_list_doc, $button_outer_container, 1, 3, array(1), false, 0, '', '');
	$button_containers = $ret_val["containers"];
	$gen_list_doc = $ret_val["doc"];
	
	$window_property_list = array("DataBinding"=> 
									array("Title"=> "Editing of ".$title));
	$window_retval = build_component("Window", $window_property_list, $window_outer_container, $semf_root, $gen_list_doc);
	
	$window_container = $window_retval["containers"];
	$gen_list_doc = $window_retval["doc"];
	
	$field_nodes = $data_node->childNodes;
	$fields_count = $field_nodes->length;
	
	// filling titles to windows top menu..
	$ret_val = new_grid_component($gen_list_doc, $window_container[0], 2, 1, array(30, -1), true, 0, '', '');
	$containers = $ret_val["containers"];
	$gen_list_doc = $ret_val["doc"];
	
	$title_container = $containers[0];
	$content_container = $containers[1];
	
	// filling the content window..
	$ret_val = new_grid_component($gen_list_doc, $title_container, 1, $fields_count +1, array(1), false, 0, '#66CCCC', '#CCFFCC');
	$field_title_containers = $ret_val["containers"];
	$gen_list_doc = $ret_val["doc"];
	
	$repeater_property_set = array("Apearance"=> array("Height"=> 22));
	$repeater_retval = build_component("Repeater", $repeater_property_set, $content_container, $semf_root, $gen_list_doc);
	$repeater_container = $repeater_retval["containers"];
	$gen_list_doc = $ret_val["doc"];
	
	$ret_val = new_grid_component($gen_list_doc, 
					$repeater_container[0], 1, $fields_count +1, array(1), false, 1,'#66CCCC', '');
	$field_content_containers = $ret_val["containers"];
	$gen_list_doc = $ret_val["doc"];
	
	for($i = 0, $field_node = $data_node->firstChild; $i < $fields_count && $field_node;
					$i ++, $field_node = $field_node->nextSibling)
	{
		//set the $ith title
		$field_title_container = $field_title_containers[$i+1];
		$field_name = $field_node->getAttribute("name");
		
		
		$field_title_property_set = array("DataBinding"=> array("Text"=> $field_name));
		$ret_val = build_component("Label", $field_title_property_set, $field_title_container, $semf_root, $gen_list_doc);
		$gen_list_doc = $ret_val["doc"];
		
		//just for now build the content same as for the title..
		$field_content_container = $field_content_containers[$i+1];
		//now check the field type..
		$field_type = $field_node->getAttribute("type");
		$field_name = $field_node->getAttribute("name");
		
		if($field_type == "Text" || $field_type == "Number" || $field_type == "Numeric" ||
			$field_type == "Date" || $field_type == "Time")
		{
			$db_code = "@all_".$data_variable.".".$field_name;
			$field_content_property_set = array("DataBinding"=> array("Text"=> $db_code));
			$ret_val = build_component("Label", $field_content_property_set,
				 $field_content_container, $semf_root, $gen_list_doc);
			
			$gen_list_doc = $ret_val["doc"];
		}
		elseif($field_type == "Image")
		{
			$db_code = "@all_".$data_variable.".".$field_name;
			$image_title = $title . " -> ".$field_name;
			$field_link_property_set = array("DataBinding"=> 
											array("Text"=> "".$field_name,
												"Title"=> "".$field_name),
											"Links"=>
											array("Link"=> $data_module.".crud_image_viewer",
												"Parameter-Count"=> 2,
												"Link-Param1"=> "src=".$db_code,
												"Link-Param2"=> "title='".$image_title."'"));
			$ret_val = build_component("Link", $field_link_property_set,
				 $field_content_container, $semf_root, $gen_list_doc);
			$gen_list_doc = $ret_val["doc"];
			
			generate_image_file($template_file, $container_id, $data_module, 
					$semf_root, $project_home);
		}
		else
		{
			$field_table_module = $field_node->getAttribute("table_module");
			$field_table = $field_node->getAttribute("table");
			if($field_table_module == "" || $field_table == "")
			{
				//some problem..
				report_error("Unknown Type.. ${field_type}");
				exit();
			}
			//add a link to view the table module..
			//new page name..
			$new_var_id = count($past_vars);
			$new_page_vars = $past_vars;
			
			$new_page_vars[$new_var_id] = array("table"=> $data_module.".".$data_table,
											"table_only"=> $data_table,
											"var"=> $data_variable,
											"attr"=> $field_name);
								
			$new_var_list_file_ptr = $field_table_module.".crud_list_".
									$data_variable."_".strtolower($field_name);
			$field_link_property_set = array("DataBinding"=> 
											array("Text"=> "".$field_name,
												"Title"=> "".$field_name),
											"Links"=>
											array("Link"=> $new_var_list_file_ptr,
												"Parameter-Count"=> count($past_vars) + 1));
			for($j = 0; $j < count($new_page_vars); $j ++)
			{
				$link_param_index = $j + 1;
				if($j == count($new_page_vars) -1)
				{
					$field_link_property_set["Links"]["Link-Param".$link_param_index] = 
						$new_page_vars[$j]["var"]."= @".$new_page_vars[$j]["var"].".id";
				}
				else
				{
					$field_link_property_set["Links"]["Link-Param".$link_param_index] = 
						$new_page_vars[$j]["var"]."= $".$new_page_vars[$j]["var"];
					
				}
			}
			$ret_val = build_component("Link", $field_link_property_set,
				 $field_content_container, $semf_root, $gen_list_doc);
			$gen_list_doc = $ret_val["doc"];
			/*generate_file($field_node, $template_file, $container_id,
						 $new_page_vars, $semf_root, $project_home);*/
		}
		
	}
	//set the radio field
	$radio_container = $field_content_containers[0]; //the very first field container.
	$radio_content_property_set = array(
						"Apearance"=> array("Single"=> "1",
											"Hidden_label"=> "1"),
						"DataBinding"=> array("List"=> "@all_${data_variable}",
												"Value"=> "\$selected_${data_variable}"));
	$ret_val = build_component("RadioGroup", $radio_content_property_set, $radio_container, $semf_root, $gen_list_doc);
	$gen_list_doc = $ret_val["doc"];
	
	//change the page on load properties on .. listings
	//now judge the startup code..
	$startup_code = "";

	
	if(count($past_vars) > 0)
	{
		$l = count($past_vars) -1;
		$lth_table = $past_vars[$l]["table"];
		$lth_var = $past_vars[$l]["var"];
		$lth_attr = $past_vars[$l]["attr"];
		$startup_code .= "SELECT FROM ${data_module}.${data_table} @${data_variable}";
		$startup_code .= " WHERE @${lth_var}.${lth_attr}=@${data_variable};\n";
		
		$startup_code .= "SELECT FROM ${data_module}.${data_table} @all_${data_variable};\n";
		
		$startup_code = "SELECT FROM ${lth_table} @${lth_var} WHERE ".
							"@${lth_var}=\$${lth_var};\n". 
									$startup_code; //something to add first
	}
	
	$startup_code .= "if(\$selected_${data_variable} == NULL)\n";
	$startup_code .= "{\n";
	$startup_code .= "  \$selected_${data_variable} = @${data_variable};\n";
	$startup_code .= "}\n";
	
	// page properties..
	$change_string = "CRUD GENERATED CODE";
	$page_property_set = array("Action Handlers"=> 
								array("onLoad"=> $startup_code));
	$ret_val = change_page_property($page_property_set, $change_string, $semf_root, $gen_list_doc, 0);
	$gen_list_doc = $ret_val["doc"];
	
	// button containers..

	//take 3rd button as Update..
	$button_property_set = array("DataBinding"=> array(
									"Text"=> "Update"));
	$ret_val = build_component("Button", $button_property_set,
				 $button_containers[2], $semf_root, $gen_list_doc);
	$gen_list_doc = $ret_val["doc"];
	
	$update_btn_id = $ret_val['component'];
	
	$update_code = "";
	if(count($past_vars) > 0)
	{
		$l = count($past_vars) -1;
		$lth_table = $past_vars[$l]["table"];
		$lth_var = $past_vars[$l]["var"];
		$lth_attr = $past_vars[$l]["attr"];
		$update_code .= "SELECT FROM ${data_module}.${data_table} @selected_${data_variable} WHERE ";
		$update_code .= "@selected_${data_variable} = \$selected_${data_variable};\n";
		$update_code .= "@${lth_var}.${lth_attr} = @selected_${data_variable};\n\n";
	}
		
	$change_string = "CRUD GENERATED CODE";
	$code_property_set = array("Action Handlers"=> 
								array("onClick"=> $update_code));
	$ret_val = change_component_property("Button", $code_property_set,
						 $update_btn_id, $change_string,
						 $semf_root, $gen_list_doc, 0);
	$gen_list_doc = $ret_val["doc"];
}



function generate_multiple_item_editing_from_existing($data_node, $gen_list_doc,
			$container_id, $title,
			$template_file, $data_variable, $past_vars, $semf_root, $project_home)
{
	
	$data_module = $data_node->getAttribute("table_module");
	$data_table = $data_node->getAttribute("table");
	
	//creating the window to listing
	$ret_val = new_grid_component($gen_list_doc, $container_id, 2, 1, array(-1,50), true, 0, '', '');
	$containers = $ret_val["containers"];
	$gen_list_doc = $ret_val["doc"];
	$window_outer_container = $containers[0];
	$button_outer_container = $containers[1];
	
	$ret_val = new_grid_component($gen_list_doc, $button_outer_container, 1, 3, array(1), false, 0, '', '');
	$button_containers = $ret_val["containers"];
	$gen_list_doc = $ret_val["doc"];
	
	$window_property_list = array("DataBinding"=> 
									array("Title"=> "Editing of ".$title));
	$window_retval = build_component("Window", $window_property_list, $window_outer_container, $semf_root, $gen_list_doc);
	
	$window_container = $window_retval["containers"];
	$gen_list_doc = $window_retval["doc"];
	
	$field_nodes = $data_node->childNodes;
	$fields_count = $field_nodes->length;
	
	// filling titles to windows top menu..
	$ret_val = new_grid_component($gen_list_doc, $window_container[0], 2, 1, array(30, -1), true, 0, '', '');
	$containers = $ret_val["containers"];
	$gen_list_doc = $ret_val["doc"];
	
	$title_container = $containers[0];
	$content_container = $containers[1];
	
	// filling the content window..
	$ret_val = new_grid_component($gen_list_doc, $title_container, 1, $fields_count +1, array(1), false, 0, '#66CCCC', '#CCFFCC');
	$field_title_containers = $ret_val["containers"];
	$gen_list_doc = $ret_val["doc"];
	
	$repeater_property_set = array("Apearance"=> array("Height"=> 22));
	$repeater_retval = build_component("Repeater", $repeater_property_set, $content_container, $semf_root, $gen_list_doc);
	$repeater_container = $repeater_retval["containers"];
	$gen_list_doc = $ret_val["doc"];
	
	$ret_val = new_grid_component($gen_list_doc, 
					$repeater_container[0], 1, $fields_count +1, array(1), false, 1,'#66CCCC', '');
	$field_content_containers = $ret_val["containers"];
	$gen_list_doc = $ret_val["doc"];
	
	for($i = 0, $field_node = $data_node->firstChild; $i < $fields_count && $field_node;
					$i ++, $field_node = $field_node->nextSibling)
	{
		//set the $ith title
		$field_title_container = $field_title_containers[$i+1];
		$field_name = $field_node->getAttribute("name");
		
		
		$field_title_property_set = array("DataBinding"=> array("Text"=> $field_name));
		$ret_val = build_component("Label", $field_title_property_set, $field_title_container, $semf_root, $gen_list_doc);
		$gen_list_doc = $ret_val["doc"];
		
		//just for now build the content same as for the title..
		$field_content_container = $field_content_containers[$i+1];
		//now check the field type..
		$field_type = $field_node->getAttribute("type");
		$field_name = $field_node->getAttribute("name");
		
		if($field_type == "Text" || $field_type == "Number" || $field_type == "Numeric" ||
			$field_type == "Date" || $field_type == "Time")
		{
			$db_code = "@all_".$data_variable.".".$field_name;
			$field_content_property_set = array("DataBinding"=> array("Text"=> $db_code));
			$ret_val = build_component("Label", $field_content_property_set,
				 $field_content_container, $semf_root, $gen_list_doc);
			
			$gen_list_doc = $ret_val["doc"];
		}
		elseif($field_type == "Image")
		{
			$db_code = "@all_".$data_variable.".".$field_name;
			$image_title = $title . " -> ".$field_name;
			$field_link_property_set = array("DataBinding"=> 
											array("Text"=> "".$field_name,
												"Title"=> "".$field_name),
											"Links"=>
											array("Link"=> $data_module.".crud_image_viewer",
												"Parameter-Count"=> 2,
												"Link-Param1"=> "src=".$db_code,
												"Link-Param2"=> "title='".$image_title."'"));
			$ret_val = build_component("Link", $field_link_property_set,
				 $field_content_container, $semf_root, $gen_list_doc);
			$gen_list_doc = $ret_val["doc"];
			
			generate_image_file($template_file, $container_id, $data_module, 
					$semf_root, $project_home);
		}
		else
		{
		/*
			$field_table_module = $field_node->getAttribute("table_module");
			$field_table = $field_node->getAttribute("table");
			if($field_table_module == "" || $field_table == "")
			{
				//some problem..
				report_error("Unknown Type.. ${field_type}");
				exit();
			}
			//add a link to view the table module..
			//new page name..
			$new_var_id = count($past_vars);
			$new_page_vars = $past_vars;
			
			$new_page_vars[$new_var_id] = array("table"=> $data_module.".".$data_table,
											"table_only"=> $data_table,
											"var"=> $data_variable,
											"attr"=> $field_name);
								
			$new_var_list_file_ptr = $field_table_module.".crud_list_".
									$data_variable."_".strtolower($field_name);
			$field_link_property_set = array("DataBinding"=> 
											array("Text"=> "".$field_name,
												"Title"=> "".$field_name),
											"Links"=>
											array("Link"=> $new_var_list_file_ptr,
												"Parameter-Count"=> count($past_vars) + 1));
			for($j = 0; $j < count($new_page_vars); $j ++)
			{
				$link_param_index = $j + 1;
				if($j == count($new_page_vars) -1)
				{
					$field_link_property_set["Links"]["Link-Param".$link_param_index] = 
						$new_page_vars[$j]["var"]."= @".$new_page_vars[$j]["var"].".id";
				}
				else
				{
					$field_link_property_set["Links"]["Link-Param".$link_param_index] = 
						$new_page_vars[$j]["var"]."= $".$new_page_vars[$j]["var"];
					
				}
			}
			$ret_val = build_component("Link", $field_link_property_set,
				 $field_content_container, $semf_root, $gen_list_doc);
			$gen_list_doc = $ret_val["doc"];
			//generate_file($field_node, $template_file, $container_id,
			//			 $new_page_vars, $semf_root, $project_home);
			*/
		}
		
	}
	//set the radio field
	$check_container = $field_content_containers[0]; //the very first field container.
	$check_content_property_set = array(
						"Apearance"=> array("Single"=> "1",
											"Hidden_label"=> "1"),
						"DataBinding"=> array("List"=> "@all_${data_variable}",
												"Value"=> "@selected_${data_variable}"));
	$ret_val = build_component("CheckBoxGroup", $check_content_property_set, $check_container, $semf_root, $gen_list_doc);
	$gen_list_doc = $ret_val["doc"];
	
	//change the page on load properties on .. listings
	//now judge the startup code..
	$startup_code = "";

	
	if(count($past_vars) > 0)
	{
		$l = count($past_vars) -1;
		$lth_table = $past_vars[$l]["table"];
		$lth_var = $past_vars[$l]["var"];
		$lth_attr = $past_vars[$l]["attr"];
		$startup_code .= "SELECT FROM ${data_module}.${data_table} @selected_${data_variable}";
		$startup_code .= " WHERE @${lth_var}.${lth_attr}=@selected_${data_variable};\n";
		
		$startup_code .= "SELECT FROM ${data_module}.${data_table} @all_${data_variable};\n";
		
		$startup_code = "SELECT FROM ${lth_table} @${lth_var} WHERE ".
							"@${lth_var}=\$${lth_var};\n". 
									$startup_code; //something to add first
	}
	
	
	// page properties..
	$change_string = "CRUD GENERATED CODE";
	$page_property_set = array("Action Handlers"=> 
								array("onLoad"=> $startup_code));
	$ret_val = change_page_property($page_property_set, $change_string, $semf_root, $gen_list_doc, 0);
	$gen_list_doc = $ret_val["doc"];
	
	// button containers..

	//take 3rd button as Update..
	$button_property_set = array("DataBinding"=> array(
									"Text"=> "Update"));
	$ret_val = build_component("Button", $button_property_set,
				 $button_containers[2], $semf_root, $gen_list_doc);
	$gen_list_doc = $ret_val["doc"];
	
	$update_btn_id = $ret_val['component'];
	
	$update_code = "";
	if(count($past_vars) > 0)
	{
		$l = count($past_vars) -1;
		$lth_table = $past_vars[$l]["table"];
		$lth_var = $past_vars[$l]["var"];
		$lth_attr = $past_vars[$l]["attr"];
		$update_code .= "@${lth_var}.${lth_attr} = @selected_${data_variable};\n\n";
	}
		
	$change_string = "CRUD GENERATED CODE";
	$code_property_set = array("Action Handlers"=> 
								array("onClick"=> $update_code));
	$ret_val = change_component_property("Button", $code_property_set,
						 $update_btn_id, $change_string,
						 $semf_root, $gen_list_doc, 0);
	$gen_list_doc = $ret_val["doc"];
}

?>
</body>
</html>
